package server;

import java.util.HashMap;

import common.User;

/**
 * Catalog of users to hold all users.
 */
public class UserCatalog {
    HashMap<String, User> users;

    /**
     * Inicializes the structure to hold the users.
     */
    public UserCatalog() {
        users = new HashMap<String, User>();
    }

    /**
     * Adds a user to the catalog.
     * 
     * @param user
     *            - user object
     * @requires !exists(user)
     */
    public void add(User user) {
        users.put(user.getName(), user);
    }

    /**
     * Removes a user from the catalog.
     * 
     * 
     * @param userName
     *            - user name to remove
     * @requires exists user.name == userName
     */
    public User remove(String userName) {
        return users.remove(userName);
    }

    /**
     * Gets a user from the catalog.
     * 
     * 
     * @param userName
     *            - user name to remove
     * @requires exists user.name == userName
     * @return user object
     */
    public User get(String userName) {
        return users.get(userName);
    }

    /**
     * Changes a user from the catalog.
     * 
     * 
     * @param user
     *            - user to change
     * @requires exists(user.name)
     */
    public void change(User user) {
        remove(user.getName());
        add(user);
    }

    /**
     * Verifies that a user is in the catalog.
     * 
     * @param userName
     *            - user name
     * @requires exists(user.name)
     * @return true is exists, otherwise false
     */
    public Boolean contains(String userName) {
        return users.containsKey(userName);
    }

    /**
     * Return the catalog size.
     * 
     * @return UserCatalog.size
     */
    public int size() {
        return users.size();
    }
}
